package com.sihai.ehr.repository;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.TrainPlanDutyTarget;
import com.sihai.ehr.model.biz.TrainPlanEmployeeTarget;

/**
 * 培训仓库
 * 
 * @author henry
 * 
 */
@Repository
public class TrainPlanRepository {
	@Resource
    private DataAccessor dataAccessor;
	
	/**
	 * 
	 * @param planId
	 * @param limit
	 * @param sorter
	 * @return
	 */
	public LimitedList<EmployeeinfoWork> getTrainPlanEmployeeinfoWorkTargets(
			final Long planId,int lectureType, Limit limit, Sorter sorter) {
		// if (planId == null || planId < 0) {
		// throw new RuntimeException("【传入参数非法】");
		// }
		try {
			
			if(lectureType < 1){
				return dataAccessor.findByQuery(EmployeeinfoWork.class,
						new QueryCreator() {
	
							@Override
							public Query createQuery(Session session) {
								DynamicQuery dynamicQuery = new DynamicQuery(
										"from EmployeeinfoWork e where e.cancel=1 and e.duty.id in (select t.duty.id from TrainPlanDutyTarget t where 1 <> 2");
								dynamicQuery.append("and t.trainPlan.id=" + planId);
								dynamicQuery.append(")");
	
								return dynamicQuery.build(session);
							}
						}, limit, sorter);
			}else{
				
				return dataAccessor.findByQuery(EmployeeinfoWork.class,
						new QueryCreator() {
							@Override
							public Query createQuery(Session session) {
								DynamicQuery dynamicQuery = new DynamicQuery(
										"from EmployeeinfoWork e where e.cancel=1 and e.id in (select t.employeeinfoWork.id from TrainPlanEmployeeTarget t where 1 <> 2");
								dynamicQuery.append("and t.trainPlan.id=" + planId);
								dynamicQuery.append(")");
	
								return dynamicQuery.build(session);
							}
						}, limit, sorter);
			}
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训职务对象时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getTrainPlanDutyTargets;参数planId="
							+ planId + "】");
		}
	}
}
